package com.fast.security.handler;

import com.fast.enums.CodeEnum;
import com.fast.pojo.AjaxResult;
import com.fast.utils.response.ResponseUtil;
import org.springframework.http.MediaType;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruan cai yuan
 * @version 1.0
 * @fileName com.fast.security.handler.AccessDeniedHandlerImpl
 * @description: 自定义访问拒绝异常返回的格式
 * @since 2024/7/13 下午8:20
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    /**
     * ExceptionTranslationFilter出现访问拒绝异常AccessDeniedException时(403状态码)会回调handle方法
     */
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        AjaxResult FORBIDDEN = AjaxResult.error(CodeEnum.FORBIDDEN.getCode(), CodeEnum.FORBIDDEN.getMsg());
        ResponseUtil.responseData(response, FORBIDDEN, MediaType.APPLICATION_JSON_VALUE);
    }
}
